/**
 * 
 */
package cn.bigcontainer.alt.meta.dto;

import cn.bigcontainer.alt.common.entity.BigMap;
import cn.bigcontainer.alt.meta.po.MApp;
import lombok.extern.slf4j.Slf4j;

/**
 * 单表单视图应用.该应用仅包含一个新建和修改视图.
 * @author cailei
 * @date 2017年6月12日
 */
@Slf4j
public class MForm extends MBasicForm {

	public static final String TYPE_CREATE = "create";

	public static final String TYPE_UPDATE = "update";

	/**
	 * 创建视图
	 */
	public static final String PARAM_CREATE = PREFIX_VIEW + TYPE_CREATE;

	/**
	 * 修改视图
	 */
	public static final String PARAM_UPDATE = PREFIX_VIEW + TYPE_UPDATE;

	public final static String PARAM_EXT_CREATE_SUBMIT = PREFIX_EXTENSION + PARAM_CREATE + ".submit";

	public final static String PARAM_EXT_UPDATE_SUBMIT = PREFIX_EXTENSION + PARAM_UPDATE + ".submit";

	public final static String PARAM_EXT_CREATE_LOAD = PREFIX_EXTENSION + PARAM_CREATE + ".load";

	public final static String PARAM_EXT_UPDATE_LOAD = PREFIX_EXTENSION + PARAM_UPDATE + ".load";

	public MForm(MApp app, BigMap<String, Object> req) {
		super(app);
		getObject().setRequest(req);
	}

	public MForm(MApp app) {
		super(app);
	}

	public MForm() {
		super();
	}

	public void setCreateView(MViewDto viewDto) {
		setView(TYPE_CREATE, viewDto);
	}

	/**
	 * @return
	 */
	public String getCreateId() {
		return this.getData().getString(PARAM_CREATE);
	}

	/**
	 * @return
	 */
	public MViewDto getCreateView() {
		return getView(TYPE_CREATE, "新建");
	}

	/**
	 *
	 * @param viewDto
	 */
	public void setUpdateView(MViewDto viewDto) {
		setView(TYPE_UPDATE, viewDto);
	}

	/**
	 * @return
	 */
	public String getUpdateId() {
		return this.getData().getString(PARAM_UPDATE);
	}

	/**
	 * @return
	 */
	public MViewDto getUpdateView() {
		return getView(TYPE_UPDATE, "修改");
	}

	/**
	 *
	 * @param fv
	 * @return
	 */
	public MViewDto beforeSubmitCreateForm (MViewDto fv) {
		return beforeSubmitForm(fv, TYPE_CREATE, true);
	}

	/**
	 * @param fv
	 * @return
	 */
	public void afterSubmitCreateForm (MViewDto fv) {
		afterSubmitForm(fv, TYPE_CREATE, true);
	}

	/**
	 * @param fv
	 * @return 如果返回非空值，则表示不再执行后续表单提交的系统代码
	 */
	public MViewDto beforeSubmitUpdateForm (MViewDto fv) {
		return beforeSubmitForm(fv, TYPE_UPDATE, false);
	}

	/**
	 * @param fv
	 * @return
	 */
	public void afterSubmitUpdateForm (MViewDto fv) {
		afterSubmitForm(fv, TYPE_UPDATE, false);
	}

	/**
	 * @param fv
	 * @return
	 */
	public MViewDto beforeLoadCreateForm (MViewDto fv, String keyValue) {
		return beforeLoadForm(fv, keyValue, TYPE_CREATE, true);
	}

	/**
	 *
	 * @param fv
	 * @param keyValue
	 */
	public void afterLoadCreateForm (MViewDto fv, String keyValue) {
		afterLoadForm(fv, keyValue, TYPE_CREATE, true);
	}

	/**
	 *
	 * @param fv
	 * @param keyValue
	 * @return 如果返回非空值，则表示不再执行后续表单加载的系统代码
	 */
	public MViewDto beforeLoadUpdateForm (MViewDto fv, String keyValue) {
		return beforeLoadForm(fv, keyValue, TYPE_UPDATE, false);
	}

	/**
	 *
	 * @param fv
	 * @param keyValue
	 */
	public void afterLoadUpdateForm (MViewDto fv, String keyValue) {
		afterLoadForm(fv, keyValue, TYPE_UPDATE, false);
	}
}
